package factory;

import java.util.HashMap;

import loadsave.LoadSaveable;
import loadsave.Memento;
import loadsave.loadsaveobject.Iterator;
import loadsave.loadsaveobject.LoadSaveMapComposite;
import loadsave.loadsaveobject.LoadSaveObject;
import loadsave.loadsaveobject.LoadSaveObjectComposite;
import loadsave.loadsaveobject.LoadSavePrimitive;
import map.Decal;

public class DecalFactory implements LoadSaveable {

    private static HashMap<String, Decal> decals;
    
    /**
     * create the static map
     */
    static {
        decals = new HashMap<String, Decal>();
    }

    /**
     * return a Decal based on the string name
     * @param decalName
     * @return
     */
    public static Decal createDecal( String decalName ) {
        return decals.get(decalName);
    }
    
    /**
     * add a Decal to the map
     * @param d
     */
    public static void addDecal(Decal d) {
        decals.put(d.getName(), d);
    }
    
    @Override
    public Memento getMemento() {
        return new Memento() {

            @Override
            public LoadSaveObject getState() {
                LoadSaveObjectComposite toReturn = new LoadSaveObjectComposite("decalFactory", "DecalFactory");
                LoadSaveMapComposite lsmc = new LoadSaveMapComposite("decals", "HashMap", "String", "Decal");
                for (String s : decals.keySet()) {
                    lsmc.add(new LoadSavePrimitive("","",s), decals.get(s).getMemento().getState());
                }
                toReturn.add(lsmc.getName(), lsmc);
                return toReturn;
            }

            @Override
            public void restoreState(LoadSaveObject lso) {
                DecalFactory.decals = new HashMap<String, Decal>();
                Iterator<LoadSaveObject> i = lso.get("decals").getIterator();
                for (i.init(); i.hasNext(); i.next()) {
                    Decal toAdd = new Decal();
                    toAdd.getMemento().restoreState(i.getCurrVal());
                    DecalFactory.decals.put(i.getCurrKey().getValue(), toAdd);
                }
            }
            
        };
    }
    
    
}
